package model;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;


public class Model {
	private LocalUser me;
	ArrayList<User> userList;// ArrayList de User
	HashMap<String,User> hm;
	ArrayList<TransferedFile> fileList;
	HashMap<String,TransferedFile> fHM;
	
	public Model(){
		this.userList = new ArrayList<User>();
		this.hm = new HashMap<String,User>();
		this.fHM = new HashMap<String,TransferedFile>();
		this.fileList = new ArrayList<TransferedFile>();
	}
	
	public void defineLocalUser (String login) {
		this.me = new LocalUser(login);
	}

	public void defineLocalAddress (String addr) {
		this.me.setAddr(addr);
	}
	
	public boolean LocalUserBind () {
		return me.getBind();
	}
	
	public String getLocalAddress () {
		return me.getAddr();
	}
	
	public String getLocalUser () {
		return me.getName();
	}

	private InetAddress getInetAddrByN (String adr) {
		InetAddress addr = null;
		try {
			addr = InetAddress.getByName(adr);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		return addr;
	}
	
		
	public String getLoginByInetAddr (InetAddress addrD) {
		if (existsInList(addrD.toString())) {
			return (hm.get(addrD.toString()).getLogin());
		}
		else {
			return "Unknown";
		}
	}
	
	public String getUserNByLogin (String login){
		int i,j =0; boolean found = false;
		for (i=0;i<login.length();i++){
			if (login.charAt(i)!='@'){
				if (!found){
				j++;}
			}
			else {
				found = true;
			}
		}
		return login.substring(0,j);
	}
	
	public String getAddrByLogin(String login){
		int i,j =0; boolean found = false;
		for (i=0;i<login.length();i++){
			if (login.charAt(i)!='@'){
				if (!found){
					j++;}
			}
			else {
				found = true;
			}
		}
		return login.substring(j+2);
	}
	
	public InetAddress[] getAddrsByName (String[] users) {
		int i=0;
		InetAddress[] addrs = new InetAddress[users.length];
		for (String u : users) {
			addrs[i]=getInetAddrByN(getAddrByLogin(u));
			i++;
		}
		return addrs;
	}
	
	
	public void addUser (String userName, String addr){
		User usr = new User(userName,addr);
		userList.add(usr);
		hm.put(addr, usr);
	}
	
	public boolean existsInList(String addr) {
		return hm.containsKey(addr);
	}

	

	public void rmvUser(InetAddress addrD) {
		int i;
		for(i=0;i<userList.size();i++){
			if (userList.get(i).getAddr()==addrD.toString()) {
				userList.remove(i);
			}
		}
		hm.remove(addrD.toString());
	}
	
	public ArrayList<User> getUserList() {
		return this.userList;
	}
		
	
	public void clearList() {
		int i ;
		for (i= userList.size();i>0;i--){
			rmvUser(getInetAddrByN(getAddrByLogin(userList.get(i-1).getLogin())));			
		}
	}


	public void addFile (String fileName, String filePath){
		TransferedFile tF = new TransferedFile(fileName,filePath);
		this.fileList.add(tF);
		this.fHM.put(fileName, tF);
		
	}

	public String getFilePath (String fileName){
		return fHM.get(fileName).getFilePath();
	}

}
